using System;
using System.Collections.Generic;
using SolutionFactory.Exporter;
using SolutionFactory.Services;
using SolutionFactory.UI;

namespace SolutionFactory_console
{
	public class ExportSolution
	{
		private readonly ILogger _logger;
		private readonly IFileSystem _fileSystem;
		private readonly FileCopyAndTokenReplacer _fileCopyAndTokenReplacer;
		private readonly FileTokenReplacer _fileTokenReplacer;

		public ExportSolution(ILogger logger,IFileSystem fileSystem,FileCopyAndTokenReplacer fileCopyAndTokenReplacer,FileTokenReplacer fileTokenReplacer)
		{
			_logger = logger;
			_fileSystem = fileSystem;
			_fileCopyAndTokenReplacer = fileCopyAndTokenReplacer;
			_fileTokenReplacer = fileTokenReplacer;
		}
		public void NewProject(string source,string name,string dir)
		{
			try
			{
				_fileSystem.Delete(dir);
				var dictionary = new Dictionary<string, string>();
				dictionary.Add("$safesolutionname$",name);
				_fileTokenReplacer.SetTokens(dictionary);
                _fileSystem.PostCopyFileVisitors.Clear();
                _fileSystem.PostCopyFileVisitors.Add(new TokenReplacerFileVisitor(_fileTokenReplacer));
                _fileSystem.PostCopyFileVisitors.Add(new RenameSolutionFileVisitor(_fileSystem, "safesolutionname",name));

				_logger.Log("Copying files");
				_fileCopyAndTokenReplacer.CopySolutionFiles(dir,source);				
				_logger.Log("New Project Completed.");
			}
			catch (Exception ex)
			{
				_logger.Log(ex.ToString());
			}
		}
		public void Export(TemplateInput model)
		{
			try
			{
				var sourceSolutionName = _fileSystem.GetFileName(model.SolutionFileName);
				var solutionSourceDirectory = _fileSystem.GetDirectoryName(model.SolutionFileName)+"\\";
				var trunkDir = _fileSystem.GetFullPath(solutionSourceDirectory.Combine(@"..\"));
				var solutionParentDirectory = solutionSourceDirectory.Replace(trunkDir, "").Replace("\\","");
				var outputDirectory = model.OutputDirectory.Combine("template") + "\\";

				InitializeFileAndFolderVisitors(sourceSolutionName, solutionParentDirectory, outputDirectory);

				_fileSystem.Delete(outputDirectory);

				_logger.Log("Copying Non-solution files");
				_fileCopyAndTokenReplacer.CopyNonSolutionFiles(outputDirectory,trunkDir, solutionParentDirectory);
				
				_fileCopyAndTokenReplacer.CopySolutionFiles(outputDirectory.Combine(solutionParentDirectory),solutionSourceDirectory);
				_logger.Log("Solution Export Completed.");
			}
			catch (Exception ex)
			{				
				_logger.Log(ex.ToString());
			}
		}

		private void InitializeFileAndFolderVisitors(string sourceSolutionName, string solutionParentDirectoryName, string destinationDirectory)
		{
			_fileSystem.PostCopyFileVisitors.Clear();
			_fileSystem.PostCopyFileVisitors.Add(new TokenReplacerFileVisitor(_fileTokenReplacer));				
			_fileSystem.PostCopyFileVisitors.Add(new SourceBinDeleterFileVisitor(solutionParentDirectoryName, "bin", "obj"));
			_fileSystem.PostCopyFileVisitors.Add(new RenameSolutionFileVisitor(_fileSystem, sourceSolutionName, "safesolutionname"));
			_fileSystem.PostCopyFileVisitors.Add(new DeleteFileTypesFileVisitor(".user", ".suo", ".cache"));
			_fileSystem.PostCopyDirectoryVisitors.Clear();
			_fileSystem.PostCopyDirectoryVisitors.Add(new DeleteDirectoriesDirectoryVisitor(destinationDirectory.Combine(solutionParentDirectoryName), "bin", "obj", "_resharper."+sourceSolutionName));
			_fileCopyAndTokenReplacer.InitializeTokenReplacer(sourceSolutionName);
		}
	}
}